﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace PhotoGallery.Controllers
{
    public class BaseController : Controller
    {
        public CultureInfo cultureInfo { get; set; }

        public BaseController()
         {
            // TODO:
             var cultureString = "BG-bg"; //GetCultureToApply();
             cultureInfo = CultureInfo.CreateSpecificCulture(cultureString);
             Thread.CurrentThread.CurrentCulture = cultureInfo;
             Thread.CurrentThread.CurrentUICulture = cultureInfo;
         }

        public void GetThumbnail(string imageUrl)
        {
            int maxHeight = 180;
            int maxWidth = 180;

            WebImage img = new WebImage(imageUrl);
            int width = img.Width;
            int height = img.Height;

            int iStart;
            Decimal divider;
            int crop;

            bool portrait = (width < height);
            if (portrait)
            {
                iStart = width;
                divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                width = maxWidth;
                height = (int)Math.Round((Decimal)(height / divider));
                crop = (height - maxHeight) / 2;
                
                img.Resize(width, height, true, true)
                    .Crop(crop, 0, crop, 0)
                    .Write();
            }
            else
            {
                iStart = height;
                divider = Math.Abs((Decimal)iStart / (Decimal)maxHeight);
                height = maxHeight;
                width = (int)Math.Round((Decimal)(width / divider));
                crop = (width - maxWidth) / 2;
                
                img.Resize(width, height, true, true)
                    .Crop(0, crop, 0, crop)
                    .Write();
            }            
        }

        public ActionResult Thumbnail(string file)
        {
            int maxHeight = 180;
            int maxWidth = 180;
            // load image
            using (Image srcImage = Image.FromFile(file))
            using (var stream = new MemoryStream())
            {

                // crop image
                int width = srcImage.Width;
                int height = srcImage.Height;

                int iStart;
                Decimal divider = 1;
                int xcrop = 0;
                int ycrop = 0;
                bool portrait = (width < height);
                if (portrait)
                {
                    iStart = height;
                    divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                    //width = maxWidth;
                    //height = (int)Math.Round((Decimal)(height / divider));
                    ycrop = (height - width) / 2;
                    height = height - 2 * ycrop;

                }
                else
                {
                    iStart = width;
                    divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                    //height = maxHeight;
                    //width = (int)Math.Round((Decimal)(width / divider));
                    xcrop = (width - height) / 2;
                    width = width - 2 * xcrop;
                }

                Rectangle cropArea = new Rectangle(xcrop, ycrop, width, height);
                using (Image tmpImage = cropImage(srcImage, cropArea))
                {

                    // resize image
                    Size size = new Size(maxWidth, maxHeight);
                    using (Image newImage = resizeImage(tmpImage, size))
                    {

                        // download image
                        newImage.Save(stream, ImageFormat.Jpeg);
                        return File(stream.ToArray(), "image/jpeg");
                    }
                }
            }
        }

        private static Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

    }
    
}
